package qos.estimator.jdbc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * This class is responsible for sending one request a time for the server,
 * simulating a single client issuing several queries.
 * 
 * @author lforleans
 *
 */
public class SerialQoSConnectionClient {
	
	
//TODO for now, using only query 0.	
	public void sendRequest() {
		try {
			Socket clientSocket = new Socket("localhost", 8888);
			PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
			out.println(0);
			out.flush();
			BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
			in.readLine();
			in.close();
			out.close();
			clientSocket.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception{
		SerialQoSConnectionClient client = new SerialQoSConnectionClient();
		int i = 1;
		while(true){
			System.out.print("Sending request #"+(i++)+"...");
			long time = System.currentTimeMillis();
			client.sendRequest();
			System.out.println("done. Processing time: " + (System.currentTimeMillis() - time)/1000f+" seconds");
		}
	}

}
